const express = require('express');
const generate_pdf = require('./utils/generatePdf');
const app = express();
const PORT = 3000;

// 中间件，用于解析 URL 中的查询参数
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

app.get('/generatepdf', async (req, res) => {
    const { contractNumber, token } = req.query;

    if (!contractNumber || !token) {
        return res.status(400).json({ code: '401', error: 'Missing contractNumber or token' });
    }

    try {
        const result = await generate_pdf(contractNumber, token);
        return res.status(200).json({ success: result });
    } catch (error) {
        console.error('Error during PDF generation:', error);
        return res.status(500).json({ error: error.toString() });
    }
});

// 处理未找到的路由
app.use((req, res) => {
    res.status(404).send('Not Found');
});

app.listen(PORT, '0.0.0.0', () => {
    console.log(`Server is running on port ${PORT}`);
});
